/*
 * dwr_packetdb.h
 *
 *  Created on: 07/05/2013
 *      Author: jose
 */

#ifndef DWR_PACKETDB_H_
#define DWR_PACKETDB_H_

#include <stdlib.h>
#include <ipq_api.h>
#include <dwr_misc.h>
//#include <dbi/dbi.h>

//#define TESIS_DEBUG

/* timestamp */
struct datetime_st
{ /* o usa time_t struct? */
	/* date */
	u16 day;
	u16 month;
	u16 year;

	/* time */
	u16 hour;
	u16 min;
	u16 sec;
};
typedef struct datetime_st DwrDatetime;

/* tcp struct */
struct tcp_st
{
	u32 source_port;
	u32 dest_port;
};
typedef struct tcp_st DwrTcp;


struct ipn_st
{
	DwrString * address;
	DwrString * netmask;
};
typedef struct ipn_st DwrIpn;

/* ip struct */
struct ip_st
{
	DwrIpn source_ip;
	DwrIpn dest_ip;
};
typedef struct ip_st DwrIp;

struct http_st
{
	DwrString * version;
	DwrString * user_agent;
	DwrString * accept;

	/* request	 */
	DwrString * method;
	DwrString * resource;
	DwrString * host;
	DwrString * cache_control;
	DwrString * date;
	DwrString * referer;
	DwrString * accept_encoding;
};
typedef struct http_st DwrHttp;

struct packet_st
{
	DwrDatetime timestamp;
	DwrTcp tcp;
	DwrIp ip;
	DwrHttp http;

	/* check if there has been data saved to struct */
	short int saved;
};
typedef struct packet_st DwrPacket;


/* FUNCTIONS */
//
//u8 *tesis_concat_comma_fields(u8 *s1, short int *previous, const u8 *s2,
//		short int text);
//
/* DB INTERACTION FUNCTIONS */
int
dwr_db_push_to_db(DwrPacket * pkt, DwrString * query);

DwrString *
dwr_db_generate_insert_query(DwrPacket * packet);
///* LOG FUNCTIONS */
//void tesis_log(u8 *file, const u8 *func, int line, u8 *description, u8 *aux);
//
///* MISC */

DwrPacket *
dwr_packet_create();

void
dwr_packet_save_string_to_struct(DwrPacket * pkt, DwrString ** field,
		const char * str, unsigned str_len);

void
dwr_packet_save_unsigned_to_struct(DwrPacket * pkt, unsigned * field,
		unsigned value);


void
dwr_packet_free_members(DwrPacket * pkt);

void
dwr_packet_zero_unsigned_members(DwrPacket * pkt);

void
dwr_packet_null_pointers(DwrPacket * pkt);


//
//void tesis_print_db_struct(tesis_st_packet *packet);
//
//void tesis_generate_insert_query(tesis_st_packet *packet);

#endif /* DWR_PACKETDB_H_ */
